clear; close all; addpath('../utilities');

if exist('jacobian.mat','file')~=2
    
    para = [0.043682298946	10.680407044771	0.031532285160	25.001413406635	3.812243088585	0.203429573303	0.102423908057	7.945920271541];

    d       = .03;
    spread  = [1-d 1+d];
    G_m     = nan(length(para),length(spread),11);
    
    for i=1:8
        
        for j=1:length(spread)
            
            para_ij = para;
            para_ij(i) = para_ij(i)*spread(j);
            
            G_m(i,j,:) = main_func_SE(para_ij);
            
        end
    end
    
   save jacobian

end

load cov_matrix_infl S_T
W       = pinv(S_T);

load cov_matrix_NW S_T N G_d

load jacobian para G_m spread

dG      = squeeze(G_m(:,2,:) - G_m(:,1,:));
jac     = dG./(para'*(spread(2)-spread(1)));

d       = jac';
var_opt = pinv( d'*pinv(S_T)*d )/N;

a       = d'*W;
var     = pinv(a*d)*a*S_T*a'*pinv(a*d)'/N;

se      = sqrt(diag(var));

row_labels = {'Idiosyncratic volatility level','Idiosyncratic volatility cyclicality','Aggregate volatility level',...
    'Aggregate volatility cyclicality','Drift scaling','Bankruptcy costs level','Bankruptcy costs cyclicality','Risk aversion'};

col_labels = {'Parameter','Estimate'};
Opts.Style = 'jf'; Opts.digit='r'; format = '%.4f';
disp(Mat2TexC(para',format,row_labels,col_labels,se,Opts,'SMM Parameter Estimates'))

load model_simulation G_m
G_T = G_d' - G_m';
J   = N*G_T'*pinv((eye(11)-d*pinv(a*d)*a)*S_T*(eye(11)-d*pinv(a*d)*a)')*G_T
